//
//  LMySettingGeneralTableViewCell.m
//  LovingPartyBuilding
//
//  Created by 刘应平 on 17/5/4.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "LMySettingGeneralTableViewCell.h"
@implementation LMySettingGeneralTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self layoutUI];
    }
    return self;
}

/**  50  */
- (void)layoutUI{
    [self addSubview:self.settingImageView];
    [self addSubview:self.settingLabel];
    [self addSubview:self.nextImageView];
    [self addSubview:self.detailLabel];
    [self addSubview:self.settingSwitch];
    self.settingSwitch.hidden = YES;
    
    self.settingImageView.sd_layout
    .topSpaceToView(self,10 * [UIScreen zj_screenHScale])
    .leftSpaceToView(self,15 * [UIScreen zj_screenWScale])
    .widthIs(30 *  [UIScreen zj_screenHScale])
    .heightIs(30 *  [UIScreen zj_screenHScale]);
    
    self.settingLabel.sd_layout
    .topSpaceToView(self,15 * [UIScreen zj_screenHScale])
    .leftSpaceToView(self.settingImageView,15 * [UIScreen zj_screenWScale])
    .heightIs(20 *  [UIScreen zj_screenHScale])
    .widthIs(200 * [UIScreen zj_screenWScale]);
    
    self.nextImageView.sd_layout
    .topSpaceToView(self,15 * [UIScreen zj_screenHScale])
    .heightIs(20 *  [UIScreen zj_screenHScale])
    .widthIs(15 * [UIScreen zj_screenWScale])
    .rightSpaceToView(self,15 * [UIScreen zj_screenWScale]);
    
    self.detailLabel.sd_layout
    .topSpaceToView(self,15 * [UIScreen zj_screenHScale])
    .heightIs(20 *  [UIScreen zj_screenHScale])
    .widthIs(100 * [UIScreen zj_screenWScale])
    .rightSpaceToView(self.nextImageView,5 * [UIScreen zj_screenWScale]);
    
    self.settingSwitch.sd_layout
    .topSpaceToView(self,5 *  [UIScreen zj_screenHScale])
    .heightIs(30 *  [UIScreen zj_screenHScale])
    .rightSpaceToView(self,15 * [UIScreen zj_screenWScale])
    .widthIs(90 * [UIScreen zj_screenWScale]);
    
    
    UIView *under = [UIView new];
    under.backgroundColor = [UIColor themeGrayColor];
    [self addSubview:under];
    under.sd_layout.bottomEqualToView(self).leftSpaceToView(self,15 * [UIScreen zj_screenWScale]).widthIs([UIScreen zj_screenWith] - 30 * [UIScreen zj_screenWScale]).heightIs(1.5);
}


/**  开关方法  */
- (void)settingSwichOnOrOff:(UISwitch *)kaiguan{
    
    if (kaiguan.isOn == YES) {
        kaiguan.onTintColor = [UIColor themeColor];
        [[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:AUTOPLAY];
    }else{
        kaiguan.onTintColor = [UIColor whiteColor];
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:AUTOPLAY];
    }
    
}


- (void)setModel:(LGeneralModel *)model{
    _model = model;
    self.settingLabel.text = model.title;
    self.detailLabel.text = [NSString stringWithFormat:@"%@MB",model.detail];
    if (model.detail.length == 0) {
        self.nextImageView.hidden = YES;
        self.detailLabel.hidden = YES;
        self.settingSwitch.hidden = NO;
    }
    self.settingImageView.image = [UIImage imageNamed:model.image];
}

- (UIImageView *)settingImageView{
    if (!_settingImageView) {
        _settingImageView = [UIImageView new];
        _settingImageView.contentMode = UIViewContentModeCenter;
    }
    return _settingImageView;
    
}

- (UIImageView *)nextImageView{
    if (!_nextImageView) {
        _nextImageView = [UIImageView new];
         _nextImageView.contentMode = UIViewContentModeRight;
        _nextImageView.image = [UIImage imageNamed:@"mine_list_pop"];
    }
    return _nextImageView;
    
}

- (UILabel *)settingLabel{
    if (!_settingLabel) {
        _settingLabel = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentLeft;
            label.textColor = [UIColor textBlackColor];
            label.backgroundColor = [UIColor clearColor];
            label.font = [UIFont systemFontOfSize:15 * [UIScreen zj_textFontScale]];
            label;
        });
    }
    return _settingLabel;
}

- (UILabel *)detailLabel{
    if (!_detailLabel) {
        _detailLabel = ({
            UILabel *label = [UILabel new];
            label.textAlignment = NSTextAlignmentRight;
            label.textColor = [UIColor lightGrayColor];
            label.backgroundColor = [UIColor clearColor];
            label.font = [UIFont systemFontOfSize:14 * [UIScreen zj_textFontScale]];
            label;
        });
    }
    return _detailLabel;
}


- (UISwitch *)settingSwitch{
    if (!_settingSwitch) {
        _settingSwitch = [[UISwitch alloc] init];
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:AUTOPLAY] isEqualToString:@"1"]) {
            _settingSwitch.on = YES;
            _settingSwitch.onTintColor = [UIColor themeColor];
        }else{
            _settingSwitch.on = NO;
            _settingSwitch.onTintColor = [UIColor whiteColor];
        }
        [_settingSwitch addTarget:self action:@selector(settingSwichOnOrOff:) forControlEvents:UIControlEventValueChanged];
    }
    return _settingSwitch;
}



@end
